package com.xman.common.comimpl;

import com.xman.handler.CodeEnum;

/**
 * 返回结果封装类
 */
public class Result<T> {
    private String code;
    private Boolean status;
    private String message;
    private T data;

    public Result() {
    }

    /**
     *
     * @param data
     */
    public Result(T data) {
        this.code = CodeEnum.SUCCESS.getResultCode();
        this.status = true;
        this.message = CodeEnum.SUCCESS.getResultMsg();
        this.data = data;
    }

    //静态方法

    /**
     * 成功
     * @param data
     * @param <T>
     * @return
     */
    public static <T>Result<T> success(T data){
        Result<T> suc = new Result<T>();
        suc.setCode("200");
        suc.setStatus(true);
        suc.setMessage("success");
        suc.setData(data);
        return suc;
    }

    /**
     * 失败
     * @param data
     * @param msg
     * @param <T>
     * @return
     */
    public static <T>Result<T> error(T data, String code, String msg){
        Result<T> err = new Result<T>();
        err.setCode(code);
        err.setStatus(false);
        err.setMessage(msg);
        err.setData(data);
        return err;
    }

    /**
     * 失败
     * @param code
     * @param msg
     * @param <T>
     * @return
     */
    public static <T>Result<T> error(String code, String msg){
        Result<T> err = new Result<T>();
        err.setCode(code);
        err.setStatus(false);
        err.setMessage(msg);
        return err;
    }

    //getter,setter
    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public Boolean getStatus() {
        return status;
    }

    public void setStatus(Boolean status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
